#include "vga_text.h"

VGATextMode::VGATextMode() : VGA() {}

void VGATextMode::Activate()
{
    unsigned char g_80x25_text[] =
    {
        /* MISC */
        0x67,
        /* SEQ */
        0x03, 0x00, 0x03, 0x00, 0x02,
        /* CRTC */
        0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
        0x00, 0x4F, 0x0D, 0x0E, 0x00, 0x00, 0x00, 0x50,
        0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
        0xFF,
        /* GC */
        0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00,
        0xFF,
        /* AC */
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
        0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
        0x0C, 0x00, 0x0F, 0x08, 0x00
    };

    WriteRegisters(g_80x25_text);

    // disable the cursor
    crtcIndexPort.Write8(0x0A);
    crtcDataPort.Write8(0x20);

    memStart = (uint16_t*)GetFrameBufferSegment();
}

void VGATextMode::SwapColors(uint32_t x, uint32_t y)
{
    uint16_t lower = *(memStart + y * WIDTH + x) & 0x0F00;
	uint16_t upper = *(memStart + y * WIDTH + x) & 0xF000;

	uint16_t newUpper = lower << 4;
	uint16_t newLower = upper >> 4;

	*(memStart + y * WIDTH + x) &= 0x00FF;
	*(memStart + y * WIDTH + x) |= (newUpper | newLower);
}